import 'dart:io';

import 'package:demo/pages/pdf/pdf_cubit.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_htmlful/Div.dart';
import 'package:flutter_htmlful/Style.dart';

class PdfPage extends StatefulWidget {
  const PdfPage({super.key});

  @override
  State<PdfPage> createState() => _PdfPageState();
}

class _PdfPageState extends State<PdfPage> with WidgetsBindingObserver {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => PdfCubit(),
      child: BlocBuilder<PdfCubit, PdfState>(
        builder: (BuildContext context, PdfState state) {
          return SafeArea(
            child: Scaffold(
              body: Div(
                style: Style(
                  backgroundColor: Colors.white,
                  height: double.infinity,
                  width: double.infinity,
                  scrollable: true,
                ),
                body: [
                  ElevatedButton(
                    child: const Text('读取'),
                    onPressed: () {
                      context.read<PdfCubit>().readPdf();
                    },
                  ),
                  Padding(padding: MediaQuery.of(context).viewInsets),
                  Text(state.pdfText),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    var size = MediaQuery.of(context).size.height;
    debugPrint("didChangeMetrics $size");
  }
}
